package com.hugh.dubbo.service.provider.com.hugh.dubbo.provide.asyncconfig;

import lombok.extern.slf4j.Slf4j;
import org.springframework.aop.interceptor.AsyncUncaughtExceptionHandler;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.AsyncConfigurer;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.Executor;

/**
 * @Author: Hugh
 * @Date: 2020/7/5
 */
@Configuration
@EnableAsync
@Slf4j
public class AsyncConfig implements AsyncConfigurer {
    @Override
    public Executor getAsyncExecutor() {
        //定义线程池
        ThreadPoolTaskExecutor threadPoolTaskExecutor = new ThreadPoolTaskExecutor();
        //设置核心线程数
        threadPoolTaskExecutor.setCorePoolSize(5);
        //设置线程池最大线程数
        threadPoolTaskExecutor.setMaxPoolSize(10);
        //设置线程队列最大线程数
        threadPoolTaskExecutor.setQueueCapacity(2000);
        //初始化线程池
        threadPoolTaskExecutor.initialize();
        log.info("线程池初始化完成。。。。");
        return threadPoolTaskExecutor;
    }

    @Override
    public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() {
        log.error("发生异常");
        return null;
    }
}
